package com.example.demo.batch.demo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.stereotype.Component;

/**
 * @author huangke
 * @description ......
 * @date 2021/1/21
 */
@Slf4j
@Component
public class StringReader implements ItemReader<String> {
    private String[] messages = {"aaa1", "aaa2", "aaa3", "aaa4"};
    private int count = 0;

    @Override
    public String read() throws UnexpectedInputException, ParseException, NonTransientResourceException {
        if (count < messages.length) {
            String message = messages[count++];
            log.debug("read data:" + message);
            return message;
        } else {
            log.debug("read data end.");
            count = 0;
        }
        return null;
    }
}